<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderCord">
      <el-input v-model="dataForm.orderCord" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="商品信息" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商品信息"></el-input>
    </el-form-item>
    <el-form-item label="买家id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="买家id"></el-input>
    </el-form-item>
    <el-form-item label="送货方式" prop="type">
      <el-input v-model="dataForm.type" placeholder="送货方式"></el-input>
    </el-form-item>
    <el-form-item label="支付金额" prop="price">
      <el-input v-model="dataForm.price" placeholder="支付金额"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="地址id" prop="addressId">
      <el-input v-model="dataForm.addressId" placeholder="地址id"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderCord: '',
          shopId: '',
          userId: '',
          type: '',
          price: '',
          status: '',
          createTime: '',
          addressId: '',
          updateTime: ''
        },
        dataRule: {
          orderCord: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商品信息不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '买家id不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '送货方式不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '支付金额不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          addressId: [
            { required: true, message: '地址id不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/system/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderCord = data.order.orderCord
                this.dataForm.shopId = data.order.shopId
                this.dataForm.userId = data.order.userId
                this.dataForm.type = data.order.type
                this.dataForm.price = data.order.price
                this.dataForm.status = data.order.status
                this.dataForm.createTime = data.order.createTime
                this.dataForm.addressId = data.order.addressId
                this.dataForm.updateTime = data.order.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/system/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderCord': this.dataForm.orderCord,
                'shopId': this.dataForm.shopId,
                'userId': this.dataForm.userId,
                'type': this.dataForm.type,
                'price': this.dataForm.price,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'addressId': this.dataForm.addressId,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
